package Server;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import Protocol.Package;

public class Server {
	static int DEFAULT_PORT = 8999;
	static Queue<Package> taskque = new LinkedList<Package>();
	static HashMap<String, Socket> clientset = new HashMap<String, Socket>();

	public static void main(String[] args) {
		ServerSocket serverSocket = null;
		try {
			serverSocket = new ServerSocket(DEFAULT_PORT);
		} catch (IOException e) {
			System.out.println("Could not listen on port: " + e);
			System.exit(1);
		}
		Connection conThread = new Connection(serverSocket);
		conThread.start();
		while (true) {
			Package pkg;
			while ((pkg = taskque.poll()) != null) {
				Task.exec(pkg);
				Socket client = clientset.get(pkg.getHost());
				BufferedWriter out;
				try {
					out = new BufferedWriter(new OutputStreamWriter(client
							.getOutputStream()));
					out.write("receieve your request");
					out.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	}

}
